/****************************************************************
Copyrighted (c) 2009 , Kenneth Nelson, Nick Marlow, www.kmtweb.nl
******************** Je weet zelluf *****************************

Based on Paul Houx's (droozle) masking solution found on the forums of openframeworks.cc

******************** sugar lee hooper **************************/

#include "ofxSimpleMask.h"

void ofxSimpleMask::setup(){

    background.loadImage("background.png");
    mask.loadImage("mask.png");
    masked.loadImage("masked.png");
}


//--------------------------------------------------------------
void ofxSimpleMask::update(){
}

//--------------------------------------------------------------
void ofxSimpleMask::draw(){
	background.draw(0, 0);

    // make sure alpha blending is enabled
	ofEnableAlphaBlending();

    // set up multi-texturing
	glActiveTexture(GL_TEXTURE0);
	masked.getTextureReference().bind();
    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
    glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);

	glActiveTexture(GL_TEXTURE1);
	mask.getTextureReference().bind();
    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
    glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_BLEND);

	// render masked and mask images as one
	glBegin(GL_QUADS);
	glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 480.0f);
	glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 480.0f);
	glVertex3f(0, 480, 0);
	glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);
	glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f);
	glVertex3f(0, 0, 0);
	glMultiTexCoord2f(GL_TEXTURE0, 640.0f, 0.0f);
	glMultiTexCoord2f(GL_TEXTURE1, 640.0f, 0.0f);
	glVertex3f(640, 0, 0);
	glMultiTexCoord2f(GL_TEXTURE0, 640.0f, 480.0f);
	glMultiTexCoord2f(GL_TEXTURE1, 640.0f, 480.0f);
	glVertex3f(640, 480, 0);
	glEnd();

    // properly unbind the textures
    mask.getTextureReference().unbind();
	glActiveTexture(GL_TEXTURE0);
	masked.getTextureReference().unbind();

	// disable alpha blending again
	ofDisableAlphaBlending();
	masked.draw(0,0);
}


//--------------------------------------------------------------
/*void ofxSimpleMask::keyPressed  (int key){
}

//--------------------------------------------------------------
void ofxSimpleMask::keyReleased(int key){

}

//--------------------------------------------------------------
void ofxSimpleMask::mouseMoved(int x, int y ){
}

//--------------------------------------------------------------
void ofxSimpleMask::mouseDragged(int x, int y, int button){
}

//--------------------------------------------------------------
void ofxSimpleMask::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofxSimpleMask::mouseReleased(int x, int y, int button){
}

//--------------------------------------------------------------
void ofxSimpleMask::resized(int w, int h){
}*/

